<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-input v-model="searchModel.username" placeholder="用户名" clearable />
          <el-input v-model="searchModel.phone" placeholder="电话" clearable />
          <el-button type="primary" @click="getUserList" round icon="el-icon-search">查询</el-button>
        </el-col>
        <el-col v-if="isSuper===1" :span="4" align="right">
          <el-button @click="editUser(null)" type="primary" icon="el-icon-plus" circle></el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 查询结果栏 -->
    <el-card>
      <el-table :data="userList" border style="width: 100%">
        <el-table-column label="#" width="50">
          <template slot-scope="scope">
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="id" label="用户ID" width="50">
        </el-table-column> -->
        <el-table-column prop="username" label="用户名" width="200">
        </el-table-column>
        <el-table-column prop="nickname" label="姓名" width="150">
        </el-table-column>
        <el-table-column prop="danwei" label="机房" width="120">
        </el-table-column>
        <el-table-column prop="zhiwu" label="职务" width="120">
        </el-table-column>
        <el-table-column prop="is_super" label="权限" width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_super == 1">管理员</el-tag>
            <el-tag v-else type="danger">普通用户</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="active" label="用户状态" width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.active == 1">正常</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_suspend" label="暂停权限" width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_suspend == 1">开启</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="email" label="电子邮件" width="300"> </el-table-column>
        <el-table-column prop="phone" label="联系电话"> </el-table-column>
        <el-table-column v-if="isSuper===1" label="编辑" width="100">
          <template slot-scope="scope">
            <el-button @click="editUser(scope.row.id)" type="primary" icon="el-icon-edit" circle size="mini"></el-button>
            <el-button @click="open(scope.row)" type="danger" icon="el-icon-delete" circle size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo" :page-sizes="[5, 10, 50, 100]" :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <!-- 弹出框 -->
    <el-dialog @close="clearForm" :title="title" :visible.sync="dialogFormVisible">
      <!-- <el-dialog :title="title" :visible.sync="dialogFormVisible"> -->
      <el-form :model="userForm" ref="userFormRef" :rules="rules">
        <el-form-item label="用户名：" prop="username" v-if="userForm.id == null || userForm.id == undefined"
          :label-width="formLabelWidth">
          <el-input v-model="userForm.username" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="姓名：" prop="nickname" :label-width="formLabelWidth">
          <el-input v-model="userForm.nickname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="职务：" prop="zhiwu" :label-width="formLabelWidth">
          <el-select v-model="userForm.zhiwu" filterable placeholder="职务">
            <el-option v-for=" item  in  zhiwuList " :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="所属机房：" prop="danwei" :label-width="formLabelWidth">
          <el-select v-model="userForm.danwei" placeholder="所属机房">
            <el-option v-for=" item  in  danwei_list " :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="密码：" prop="password" v-if="userForm.id == null || userForm.id == undefined"
          :label-width="formLabelWidth">
          <el-input type="password" v-model="userForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话：" prop="phone" :label-width="formLabelWidth">
          <el-input v-model="userForm.phone" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="超级权限：" :label-width="formLabelWidth">
          <el-switch v-model="userForm.is_super" :active-value="1" :inactive-value="0">
          </el-switch>
        </el-form-item>

        <el-form-item label="用户状态：" :label-width="formLabelWidth">
          <el-switch v-model="userForm.active" :active-value="1" :inactive-value="0">
          </el-switch>
        </el-form-item>

        <el-form-item label="暂停权限：" :label-width="formLabelWidth">
          <el-switch v-model="userForm.is_suspend" :active-value="1" :inactive-value="0">
          </el-switch>
        </el-form-item>

        <el-form-item label="电子邮箱：" prop="email" :label-width="formLabelWidth">
          <el-input v-model="userForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像：" prop="image" :label-width="formLabelWidth">
          <el-input v-model="userForm.image" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button @click="addUser" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import userApi from "@/api/userManage";
import { mapGetters } from 'vuex';
export default {
  data() {
    // var checkEmail = (rule, value, callback) => {
    //   var reg =
    //     /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
    //   if (!reg.test(value)) {
    //     return callback(new Error("邮箱格式输入有误"));
    //   }
    //   callback();
    // };
    // var checkPhone = (rule, value, callback) => {
    //   var reg =
    //     /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    //   if (!reg.test(value)) {
    //     return callback(new Error("电话格式输入有误"));
    //   }
    //   callback();
    // };
    return {
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
        nickname: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          {
            trigger: "blur",
          },
        ],
        zhiwu: [
          { required: true, message: "请输入职务", trigger: "blur" },
          {
            trigger: "blur",
          },
        ],
        danwei: [
          { required: true, message: "请输入所属机房", trigger: "blur" },
          {
            trigger: "blur",
          },
        ],
        image: [
          { required: true, message: "可以输入网图", trigger: "blur" },
          {
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
        // phone: [
        //   { required: true, message: "请输入联系电话", trigger: "blur" },
        //   { validator: checkPhone, trigger: "blur" },
        // ],
        // email: [
        //   { required: true, message: "请输入电子邮箱", trigger: "blur" },
        //   { validator: checkEmail, trigger: "blur" },
        // ],
      },
      userForm: {
        // "username": "ceshi6",
        // "nickname": "测试",
        "zhiwu": "工程师",
        "danwei": "建德机房",
        // "password": "sftp64526873",
        // "phone": "13750890761",
        // "super": 1,
        "active": 1,
        // "email": "hzshenpeng@corp.netease.com",
        "image": "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"
      },
      formLabelWidth: "130px",
      dialogFormVisible: false,
      title: "",
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 5,
      },
      depModel: {
        pageNo: 1,
        pageSize: 100,
      },
      userList: [],
      zhiwuList: [{
        value: '经理',
        label: '经理'
      }, {
        value: '主管',
        label: '主管'
      },{
        value: '组长',
        label: '组长'
      },{
        value: '工程师',
        label: '工程师'
      }],
      danwei_list: [
      //   {
      //   value: '建德机房',
      //   label: '建德机房'
      // }, {
      //   value: '义桥机房',
      //   label: '义桥机房'
      // }, {
      //   value: '聚园路机房',
      //   label: '聚园路机房'
      // }, {
      //   value: '滨安机房',
      //   label: '滨安机房'
      // }, {
      //   value: '富春云机房',
      //   label: '富春云机房'
      // }, {
      //   value: '网易园区',
      //   label: '网易园区'
      // }
    ],
    };
  },
  methods: {
    open(user) {
      this.$confirm(`您是否确认删除用户${user.username}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          userApi.deleteUserById(user.id).then((response) => {
            this.$message({
              type: "success",
              message: response.message,
            });
            this.getUserList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addUser() {
      // 表单验证
      this.$refs.userFormRef.validate((valid) => {
        if (valid) {
          // 提交表单到后台
          userApi.saveUser(this.userForm).then((reponse) => {
            // 关闭弹窗
            this.dialogFormVisible = false;
            // 提示提交成功
            this.$message({
              message: reponse.message,
              type: "success",
            });
            // 刷新页面
            this.getUserList();
            // 刷新整个页面
            // window.location.reload();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    clearForm() {
      this.userForm = {
        // "username": "ceshi6",
        // "nickname": "测试",
        "zhiwu": "工程师",
        "danwei": "建德机房",
        // "password": "sftp64526873",
        // "phone": "13750890761",
        // "super": 1,
        "active": 1,
        // "email": "hzshenpeng@corp.netease.com",
        "image": "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"
      };
      this.$refs.userFormRef.clearValidate();
    },
    editUser(id) {
      if (id == null) {
        this.title = "新增用户";
        //先从后端获取idc机房信息
        userApi.getDepList(this.depModel).then((reponse) => {
          this.danwei_list = reponse.data.api_dep_list;
        });
      } else {
        this.title = "修改用户";
        //先从后端获取idc机房信息
        userApi.getDepList(this.depModel).then((reponse) => {
          this.danwei_list = reponse.data.api_dep_list;
        });
        //先从后端获取user信息
        userApi.getUserById(id).then((reponse) => {
          this.userForm = reponse.data;
        });
      }
      this.dialogFormVisible = true;
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getUserList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getUserList();
    },
    getUserList() {
      userApi.getUserList(this.searchModel).then((response) => {
        this.total = response.data.total;
        this.userList = response.data.user;
      });
    },
  },
  created() {
    this.getUserList();
  },
  computed:{
    ...mapGetters([
      // 'name',
      'isSuper',
      // 'nick_name'
    ]),
  }
};
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}

.el-dialog .el-input {
  width: 88%;
}
</style>